﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Navigation;

namespace NO.I
{
    /// <summary>
    /// OAuthWindow.xaml 的交互逻辑
    /// </summary>
    public partial class OAuthWindow : Window
    {

        private static string _code = string.Empty;
        private static string _client_ID = "3414937649";
        private static string _client_SERCRET = "5b7356e24f734e5b64175b48c0814faa";
        private static string _redirect_URI = "http://www.anntasy.com/SinaCallBack";
        private static string _accessTokenURL = "https://api.weibo.com/2/oauth2/access_token";
        private static string _authorizeURL = "https://api.weibo.com/2/oauth2/authorize";

        public static readonly DependencyProperty OAuthInfoProperty = DependencyProperty.Register("OAuthInfo", typeof(Dictionary<string, string>), typeof(OAuthWindow));

        public event EventHandler AuthCompleted;

        public Dictionary<string, string> OAuthInfo
        {
            get
            {
                return (Dictionary<string, string>)this.GetValue(OAuthInfoProperty);
            }
            set
            {
                this.SetValue(OAuthInfoProperty, value);
            }
        }

        public OAuthWindow()
        {
            InitializeComponent();

            StringBuilder sb = new StringBuilder(_authorizeURL);
            sb.AppendFormat("?client_id={0}", _client_ID);
            sb.AppendFormat("&redirect_uri={0}", _redirect_URI);
            authWindow.Navigate(sb.ToString());
        }

        private void authWindow_Navigated(object sender, NavigationEventArgs e)
        {
            Uri uri = e.Uri;
            _code = HttpUtility.ParseQueryString(uri.Query).Get("code");

            if (!string.IsNullOrEmpty(_code))
            {

                StringBuilder sb = new StringBuilder(_accessTokenURL);
                sb.AppendFormat("?client_id={0}&client_secret={1}&redirect_uri={2}&grant_type={3}&code={4}", _client_ID, _client_SERCRET, _redirect_URI, "authorization_code", _code);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
                request.Method = "POST";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();

                string result = string.Empty;

                using (StreamReader streamReader = new StreamReader(stream))
                {
                    result = streamReader.ReadToEnd();
                }

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, string> json = serializer.Deserialize<Dictionary<string, string>>(result);
                OAuthInfo = json;

                if (AuthCompleted != null)
                {
                    AuthCompleted(this, null);
                    this.Close();
                }
            }
        }

        private void authWindow_LoadCompleted(object sender, NavigationEventArgs e)
        {
            mshtml.HTMLDocument dom = (mshtml.HTMLDocument)authWindow.Document; //定义HTML
            dom.documentElement.style.overflow = "hidden";                      //隐藏浏览器的滚动条
            dom.body.setAttribute("scroll", "no");
        }
    }
}
